package com.feihong.txgw.ui.activity.integral;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.adapter.base.BaseQuickAdapter;
import com.feihong.txgw.R;
import com.feihong.txgw.adapter.user.IntegralDetailReAdapter;
import com.feihong.txgw.inter.contract.integral.IIntegralContract;
import com.feihong.txgw.model.bean.integral.IntegralBackEntity;
import com.feihong.txgw.tx_precenter.IntegralMainPrecenter;
import com.feihong.txgw.ui.mvp.BaseMVPCompateActivity;
import com.feihong.txgw.ui.mvp.precenter.AbBasePrecenter;
import java.util.List;

import butterknife.BindView;

/**
 * 积分明细
 */
public class IntegralDetailActivity extends BaseMVPCompateActivity<IIntegralContract.AbIntegralMainPrecenter> implements
        IIntegralContract.IIntegralMainView, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.lv_jifenlist)
    RecyclerView lvJifenlist;
    @BindView(R.id.freshlayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.lay_loading)
    RelativeLayout mLoadLayout;
    @BindView(R.id.re_query_data_layout)
    LinearLayout mRequeryDataBtn;
    @BindView(R.id.integralDetail_no_layout)
    LinearLayout mNullLayout;
    @BindView(R.id.connect_net_error_layout)
    LinearLayout mConnectErrorLayout;

    private boolean isHasData = false;
    private boolean isFullPage = false;
    private IntegralDetailReAdapter mAdapter;
    private LinearLayoutManager linearLayoutManager;

    @Override
    public void setTheme() {

    }

    @Override
    public int setLayoutID() {
        return R.layout.activity_integral_detail;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setBackButtonFinish();
        setPageTitle("积分明细");
        mSwipeRefreshLayout.setColorSchemeColors(Color.rgb(223, 47, 47));
        mNullLayout.setVisibility(View.GONE);
        mConnectErrorLayout.setVisibility(View.GONE);
        linearLayoutManager = new LinearLayoutManager(this);
        lvJifenlist.setLayoutManager(linearLayoutManager);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isHasData = false;
                mPresenter.loadIntegralDataFirst(IntegralDetailActivity.class.getName(), "1");

            }
        });
        mRequeryDataBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mConnectErrorLayout.setVisibility(View.GONE);
                mLoadLayout.setVisibility(View.VISIBLE);
                getData();
            }
        });
        initAdaper();
    }

    private void initAdaper() {
        mAdapter = new IntegralDetailReAdapter(R.layout.item_integraldetail);
        lvJifenlist.setAdapter(mAdapter);
    }

    @Override
    public void bindData() {
        super.bindData();
        getData();
    }

    @Override
    public void setBackIntegralData(boolean isFullPage,List<IntegralBackEntity.DataBean.PageListBean> integralBackEntity) {
        this.isFullPage = isFullPage;
        if(isFullPage){
            mAdapter.setOnLoadMoreListener(this, lvJifenlist);
        }
        Log.i(TAG, "setBackIntegralData: 有数据   是否满页  " + isFullPage);
        mSwipeRefreshLayout.setVisibility(View.VISIBLE);
        mNullLayout.setVisibility(View.GONE);
        mConnectErrorLayout.setVisibility(View.GONE);
        if(isHasData){
           mAdapter.addData(integralBackEntity);
        }else {
            isHasData = true;
            mAdapter.setNewData(integralBackEntity);
        }
        mAdapter.loadMoreComplete();
    }

    @Override
    public void notData() {
        Log.i(TAG, "notData: 无数据");
        mAdapter.loadMoreEnd();
        if (!isHasData) {
            mSwipeRefreshLayout.setVisibility(View.GONE);
            mNullLayout.setVisibility(View.VISIBLE);
            mConnectErrorLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void dataOtherError(String errorMsg) {
        mAdapter.loadMoreFail();
        if (!isHasData) {
            mSwipeRefreshLayout.setVisibility(View.GONE);
            mNullLayout.setVisibility(View.VISIBLE);
            mConnectErrorLayout.setVisibility(View.GONE);
        }
        showSystemToast(errorMsg);
    }

    @Override
    public void loadDataComplated() {
        mLoadLayout.setVisibility(View.GONE);
        finishRefreshing();
    }

    @Override
    public void loadDataError() {
        finishRefreshing();
        mAdapter.loadMoreFail();
        mLoadLayout.setVisibility(View.GONE);
        if (isHasData) {
            showSystemToast(getString(R.string.connect_service_error));
        } else {
            mSwipeRefreshLayout.setVisibility(View.GONE);
            mNullLayout.setVisibility(View.GONE);
            mConnectErrorLayout.setVisibility(View.VISIBLE);
        }
    }

    @NonNull
    @Override
    public AbBasePrecenter initPresenter() {
        return IntegralMainPrecenter.create();
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMoreRequested() {
        getData();
    }

    private void finishRefreshing() {
        if (mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(false);
        }

        if(mLoadLayout.getVisibility() == View.VISIBLE){
            mLoadLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 加载积分列表
     */
    private void getData() {
        mPresenter.loadBackIntegralData(IntegralDetailActivity.class.getName(), "1");
    }
}
